package com.hntxk.cqzd.sms.action;

import com.hntxk.core.framework.model.OperModel;
import com.hntxk.core.framework.model.ResponseModel;
import com.hntxk.core.framework.model.ResultModel;
import com.hntxk.core.util.JsonUtil;
import com.hntxk.cqzd.common.action.CqzdBaseController;
import com.hntxk.cqzd.sms.model.SmsInfo;
import com.hntxk.cqzd.sms.service.SmsInfoService;
import io.swagger.annotations.*;
import org.apache.logging.log4j.LogManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

/**
 *  手机短信发送controller类
 *  
 * @author Huang.Biquan
 * @date 2019年10月17日 10点09分
 */
@Api(value="手机短信发送控制器", tags={"[CQZD]手机短信接口"})
@Controller
@RequestMapping("cqzd/do/sms")
public class SmsInfoController extends CqzdBaseController {


	private final SmsInfoService service;
	@Autowired
	public SmsInfoController(@Qualifier("smsInfoServiceImpl") SmsInfoService service) {
		super(service, LogManager.getLogger(SmsInfoController.class));
		this.service = service;
	}


	/**
	 * 获取手机短信内容
	 *
	 * @return
	 */
    @ApiOperation(value="获取手机短信内容",notes = "产前诊断系统")
	@ApiImplicitParam(name = "id", value = "短信id",example = "1",required = true, dataTypeClass = Long.class)
	@GetMapping(value = "/get/{id}", produces= PRODUCE_UTF8_JSON)
	@ResponseBody
	public String get(@PathVariable("id") String id){
		SmsInfo smsInfo = service.get(toLong(id));
		return toJson(smsInfo);
	}
	
	
	/**
	 * 手机短信发送历史列表数据
	 *
	 * @param infoContent 部分消息内容【模糊查找】
	 * @return
	 */
	@ApiOperation(value="手机短信发送列表数据",notes = "产前诊断系统")
	@ApiImplicitParam(name = "infoContent", value = "部分消息内容【模糊查找】",example = "你好", dataTypeClass = String.class)
	@RequestMapping(value = "/list", method = {RequestMethod.GET,RequestMethod.POST}, produces= PRODUCE_UTF8_JSON)
	@ResponseBody
	public ResponseModel list(@RequestParam(required = false) String infoContent) {
		SmsInfo smsInfo = new SmsInfo();
		smsInfo.setInfoContent(infoContent);
		smsInfo.setPagination(request);
		return service.query2Json(smsInfo,true);
	}

	/**
	 *  发送手机短信
	 * @param model
	 * @return
	 */
	@ApiOperation(value="发送手机短信",notes = "产前诊断系统")
	@ApiImplicitParam(name = "json", value = "JSON字符串参数",required = true, dataTypeClass = String.class,example = "{\n" +
			"    \"infoContent\": \"\",\n" +
			"    \"sendTime\": \"\",\n" +
			"    \"smsSendee\": [\n" +
			"        {\n" +
			"            \"sendeeId\": \"\",\n" +
			"            \"sendeeName\": \"\",\n" +
			"            \"branchId\": \"\",\n" +
			"            \"branchName\": \"\"\n" +
			"        }\n" +
			"    ]\n" +
			"}")
	@PostMapping(value = "/send", produces= PRODUCE_UTF8_JSON)
	@ResponseBody
	public String send(@RequestBody SmsInfo model){

		/*//创建HttpClient对象
		CloseableHttpClient httpClient = HttpClients.createDefault();
		//创建HttpGet请求
		HttpPost httpPost = new HttpPost("http://localhost:8008/commom/file/ces");
		//声明存放参数的List集合
		List<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair("msgid", "1"));
		params.add(new BasicNameValuePair("tel", "15173179162"));
		params.add(new BasicNameValuePair("msgcontent", "1"));
		params.add(new BasicNameValuePair("name", "1"));
		params.add(new BasicNameValuePair("departid", "1"));
		params.add(new BasicNameValuePair("key", "1"));
		//创建表单数据Entity
		UrlEncodedFormEntity formEntity = null;
		try {
			formEntity = new UrlEncodedFormEntity(params, "UTF-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		//设置表单Entity到httpPost请求对象中
		httpPost.setEntity(formEntity);
		CloseableHttpResponse response = null;
		try {
			//使用HttpClient发起请求
			response = httpClient.execute(httpPost);

//			解析dom
			String content = EntityUtils.toString(response.getEntity(), "UTF-8");
			Document doc = DocumentHelper.parseText(content);
			Element rootElt = doc.getRootElement();

			if (!"ok".equals(rootElt.getText())){
				if("key未授权".equals(rootElt.getText())){
					return responseSorry("key未授权");
				}else {
					return responseSorry("短信唯一标识号已存在，请重新输入");
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			//释放连接
			if (response == null) {
				try {
					response.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
				try {
					httpClient.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}*/
		ResultModel<SmsInfo> resultModel = service.insert(model, new OperModel());
		return resultModel.hasError()?responseSorry(resultModel.getErrMsg()):responseOk();
	}
}
